#pragma once

#include <boost\signal.hpp>

#include <vector>
#include <map>
#include <tuple>
#include <iostream>

#include <opencv\highgui.h>

#include "Marker.h"

#include "RenderSystem.h"

#include "Game.h"
#include "GameBoard.h"
#include "GameBoardGenerator.h"
#include "MarkerEnvironment.h"

using namespace TUMAR::MarkerTracking;

namespace TUMAR { namespace CountryRoads {
	
	class MarkerHistory
	{
	private:
		bool HasChanged(int x, int y);
	public:
		int X;
		int Y;
		double Timestamp;

		MarkerHistory(void);		
		~MarkerHistory(void);

		void Update(int x, int y);

		bool LockIntervalExpired(double interval);
	};

	class GameController;

	class MovementTimer : public TUMAR::Render::TimerCallback
	{
	private:
		GameController* controller;
	public:
		MovementTimer(GameController* controller);
		virtual ~MovementTimer(void);
		virtual void Tick(void) const;
	};
	
	class StartMovementTimer : public TUMAR::Render::TimerCallback
	{
	private:
		GameController* controller;
	public:
		StartMovementTimer(GameController* controller);
		virtual ~StartMovementTimer(void);
		virtual void Tick(void) const;
	};

	class GameController
	{
	private:
		StartMovementTimer startTimer;
		MovementTimer timer;

		Game* game;
		GameBoard* board;
		MarkerEnvironment* markerEnvironment;

		std::map<int, MarkerHistory> history;

		double startTime;

		void CheckGameState(GameState expected);

		void PrintTimeLeft(void);
	public:
		boost::signal<void (const Game* game, const GameBoard* board, const MarkerEnvironment* marker)> GameUpdated;

		GameController(Game* game, GameBoard* board, MarkerEnvironment* markerEnvironment);
		~GameController(void);

		void InitGame(void);

		void StartGame(void);
		void StartMoveCharacter(void);
		void MoveCharacter(void);
		void FinishLostGame(void);
		void FinishWonGame(void);

		void OnKeyPressed(unsigned char key);
		void OnMarkersFound(const MarkerContainer& markers);
	};

}}